package com.zzyl.nursing.controller.member;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.core.domain.R;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.common.utils.UserThreadLocal;
import com.zzyl.nursing.domain.Reservation;
import com.zzyl.nursing.dto.ReservationDto;
import com.zzyl.nursing.service.IMemberReservationService;
import com.zzyl.nursing.vo.TimeCountVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 预约信息Controller
 *
 * @author ruoyiJ
 */
@RestController
@RequestMapping("/member/reservation")
@Api(tags = "预约信息相关接口")
public class MemberReservationController extends BaseController {
    @Autowired
    private IMemberReservationService reservationService;

    /**
     * 查询当天取消预约数量
     */
    @GetMapping("/cancelled-count")
    @ApiOperation("查询当天取消预约数量")
    public R<Integer> getCancelledReservationCount() {
        // 获取当前登录用户ID
        Long userId = UserThreadLocal.getUserId();

        long time = System.currentTimeMillis();
        LocalDateTime ldt = LocalDateTimeUtil.of(time);
        LocalDateTime startTime = ldt.toLocalDate().atStartOfDay();
        LocalDateTime endTime = startTime.plusDays(1);

        // 查询当天取消的预约数量（状态为2表示取消）
        int count = reservationService.countCancelledByUserAndTime(userId, startTime, endTime);
        return R.ok(count);
    }

    @GetMapping("/countByTime")
    @ApiOperation("查询每个时间段剩余预约次数")
    public R<List<TimeCountVo>> countByTime(
            @ApiParam(value = "日期时间戳", required = true)
            @RequestParam long time) {
        List<TimeCountVo> result = reservationService.countRemainingByTime(time);
        return R.ok(result);
    }
    @PostMapping
    @ApiOperation("添加预约信息")
    public AjaxResult insertReservation(@RequestBody ReservationDto reservationDto) {
        // 获取当前登录用户ID
        Long userId = UserThreadLocal.getUserId();
        //如果同一个人当天取消预约超过三次,则不能再预约
        if(reservationService.countCancelledByUserAndTime(userId, LocalDateTime.now(), LocalDateTime.now().plusDays(1)) >= 3){
            return error("您当天取消预约次数过多，请勿重复预约");
        }
        reservationService.insertReservation(reservationDto);
        return success();
    }
    /**
     * 分页查询预约信息
     */
    @GetMapping("/page")
    @ApiOperation("分页查询预约信息")
    public R<TableDataInfo<List<Reservation>>> page(Reservation reservation) {
        startPage();
        List<Reservation> list = reservationService.selectReservationList(reservation);
        return R.ok( getDataTable( list));
    }

    /**
     * 取消预约信息
     */
    @PutMapping("/{id}/cancel")
    public AjaxResult deleteReservation(@PathVariable Long id) {
        //根据id查询预约信息
        Reservation reservation = reservationService.getById(id);
        //判断预约信息是否存在
        if(reservation == null){
            return error("预约信息不存在");
        }
        //设置预约状态为取消
        reservation.setStatus(2);
        reservation.setUpdateBy(UserThreadLocal.getUserId().toString());
        reservationService.updateById(reservation);
        return success();
    }
}